// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import endingMediator from "./endingMediator";
import AudioManage, {soundList} from "../framework/AudioManage";
import sceneMediator from "../framework/sceneMediator";
import {toolList} from "./handControl";
import Tool from "../framework/Tool";
import roleMediator from "./roleMediator";
import gameData from "./gameData";
// import AudioManage, {soundList} from "../resources/framework/AudioManage";
// import sceneMediator from "../resources/framework/sceneMediator";

const {ccclass, property} = cc._decorator;

export enum actionType {
    pass = 0,
    nextLevel = 1,
    open = 2,
    close = 3,
    moveToPos = 4,
    showAction = 5
}

const  ShaderEnum = cc.Enum({});

@ccclass
export default class portalMediator extends cc.Component {

    @property({
        type : cc.Enum(actionType),
        displayName : "行为类型"
    })
    curAction : actionType = actionType.pass;

    // @property({
    //     type: ShaderEnum,
    // })
    @property()
    sceneName : string = "";

    @property({type : cc.Node,displayName : "角色"})
    roleNode : cc.Node = null;

    @property({type : cc.Node,displayName : "传送到的位置"})
    portalNode : cc.Node = null;

    @property({type : cc.Component.EventHandler,displayName : "触发事件"})
    toolEvent : cc.Component.EventHandler[] = [];

    @property()
    openUpdate : boolean = false;

    curStatus : boolean = false;

    isCrash = false;
    onBeginContact(contact, self, other) {
        if(this.openUpdate && (other.node.group == "role" || other.node.group == "ghost")){
            this.isCrash = true;
            console.log("1111");
        }
        this.node.getChildByName("bg").active = true;
    }

    onEndContact(contact, self, other) {
        if(this.openUpdate && (other.node.group == "role" || other.node.group == "ghost")){
            this.isCrash = false;
        }
        this.node.getChildByName("bg").active = false;
    }

    checkDoor(){
        cc.game.emit("setCurCheckNode",this);
        let type = this.node.getComponent("toolTypeList").toolType;
        cc.game.emit("setHandStatus",true,type,this);
    }

    portalAction(){
        switch (this.curAction) {
            case actionType.pass :
                cc.game.emit("gameWin");
                break;
            case  actionType.nextLevel :
                AudioManage.playSFX(soundList.switchRoom,false);
                // let list = endingMediator.getSceneList();
                if(sceneMediator.getShowScene() == 10 &&  !gameData.backToGame){
                    AudioManage.playLevelMusic(soundList.vortex, true);
                }
                cc.director.loadScene("game" + this.sceneName);
                gameData.backToGame = true;
                break;
            case actionType.open :
                this.curStatus = cc.find("Canvas/bg").active;
                this.curStatus = !this.curStatus;
                if(this.curStatus){
                    cc.find("Canvas/table").getComponent(cc.PhysicsBoxCollider).enabled = true;
                    cc.find("Canvas/bg").active = true;
                    cc.find("Canvas/bg1").active = false;
                    cc.find("Canvas/table").getComponent("actionMediator").setTableSkin(true);
                }else{
                    cc.find("Canvas/table").getComponent(cc.PhysicsBoxCollider).enabled = false;
                    cc.find("Canvas/bg").active = false;
                    cc.find("Canvas/bg1").active = true;
                    cc.find("Canvas/table").getComponent("actionMediator").setTableSkin(false);
                }
                break;
            case actionType.close :
                this.curStatus = cc.find("Canvas/bg").active;
                this.curStatus = !this.curStatus;
                if(this.curStatus){
                    cc.find("Canvas/table").getComponent(cc.PhysicsBoxCollider).enabled = true;
                    cc.find("Canvas/bg").active = true;
                    cc.find("Canvas/bg1").active = false;
                    cc.find("Canvas/table").getComponent("actionMediator").setTableSkin(true);
                }else{
                    cc.find("Canvas/table").getComponent(cc.PhysicsBoxCollider).enabled = false;
                    cc.find("Canvas/bg").active = false;
                    cc.find("Canvas/bg1").active = true;
                    cc.find("Canvas/table").getComponent("actionMediator").setTableSkin(false);
                }
                break;
            case actionType.moveToPos :
                if(sceneMediator.getShowScene() == 17){
                    if(this.sceneName == "1"){
                        cc.game.emit("gameReserve",1);
                        cc.Camera.main.node.y += cc.winSize.height;
                    }else if(this.sceneName == "2"){
                        cc.Camera.main.node.y -= cc.winSize.height;
                        cc.game.emit("gameReserve",2);
                    }
                    this.roleNode.position = this.portalNode.position;
                    let pos1 = this.roleNode.getChildByName("petNode1").convertToWorldSpaceAR(cc.v2(0,0));
                    let pos11 = cc.find("Canvas/petGroup").convertToNodeSpaceAR(pos1);
                    cc.find("Canvas/petGroup").getChildByName("pet1").x = pos11.x;
                    cc.find("Canvas/petGroup").getChildByName("pet1").y = pos11.y;

                    let pos2 = this.roleNode.getChildByName("petNode2").convertToWorldSpaceAR(cc.v2(0,0));
                    let pos22 = cc.find("Canvas/petGroup").convertToNodeSpaceAR(pos2);
                    cc.find("Canvas/petGroup").getChildByName("pet2").x = pos22.x;
                    cc.find("Canvas/petGroup").getChildByName("pet2").y = pos22.y;
                }else if(sceneMediator.getShowScene() == 10 && gameData.getPetCurSkin().length == 1 && gameData.getPetCurSkin()[0] == 1 && !gameData.openTimesSwtich && sceneMediator.curEnterLevel == 0){
                    gameData.openTimesSwtich = true;
                    roleMediator.LastRolePosition = cc.v2(this.roleNode.x,this.roleNode.y);
                    Tool.loadPrefab("prefabs/enterTimes",(res)=>{
                        let view = cc.instantiate(res);
                        view.parent = cc.find("Canvas");
                        view.zIndex = 5000;
                    })
                }else{
                    this.roleNode.getComponent("roleMediator").isCameraMoving = true;
                    this.roleNode.position = this.portalNode.position;
                    this.roleNode.getComponent("roleMediator").initLocation = cc.v2(this.portalNode.x,this.portalNode.y);
                    this.scheduleOnce(()=>{
                        this.roleNode.getComponent("roleMediator").isCameraMoving = false;
                    })
                }
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
                break;
            case actionType.showAction :
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
                break;
        }
    }

    protected update(dt: number): void {
        if(this.openUpdate && this.isCrash){
            this.checkDoor();
        }
    }
}
